/*
 * Copyright (c) 2013-2015 Charkey. All rights reserved.
 *
 * This software is the confidential and proprietary information of Charkey.
 * You shall not disclose such Confidential Information and shall use it only
 * in accordance with the terms of the agreements you entered into with Charkey.
 *
 * Charkey MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE SOFTWARE,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
 *
 * Charkey SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING,
 * MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 */

package cn.simastudio.talos.common.authc.hmac;

import cn.simastudio.talos.common.constants.SimaConstants;
import cn.simastudio.talos.common.utils.hmac.HmacAuthUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Usage:
 *
 * @author Charkey
 * @date 2015/5/19 9:12
 */
public class HmacAuthRequest {

    private String id;

    private String nonce;

    private String mac;

    private HttpServletRequest request;

    public HmacAuthRequest(HttpServletRequest request) {
        this.request = request;
        decodeClientAuthenticationHeader(request.getHeader("Authorization"));
    }

    public HttpServletRequest getRequest() {
        return request;
    }

    public String getId() {
        return id;
    }

    public String getNonce() {
        return nonce;
    }

    public String getMac() {
        return mac;
    }

    private void decodeClientAuthenticationHeader(String authenticationHeader) {
        Map<String, String> macMap = HmacAuthUtils.decodeClientAuthenticationHeader(authenticationHeader);
        if (macMap != null) {
            this.id = macMap.get(SimaConstants.Auth.AUTH_ID);
            this.nonce = macMap.get(SimaConstants.Auth.AUTH_NONCE);
            this.mac = macMap.get(SimaConstants.Auth.AUTH_MAC);
        }
    }


}
